package com.lizhe.exception.controller;

import com.lizhe.exception.modle.AjaxResponse;
import com.lizhe.exception.modle.Article;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 类描述: 增删改查
 *
 * @author Lz on 2021-10-28
 */
@RestController
@RequestMapping("/rest")
class TestController {

    // 使用@Valid来开启入参校验  捕获异常MethodArgumentNotValidException
    @PostMapping("/list")
    public AjaxResponse list(@Valid @RequestBody Article article) {

        // try {
        //     int age = 1 / 0;
        //     System.out.println(age);
        // } catch (Exception e) {
        //     throw new CustomException(CustomExceptionType.SYSTEM_ERROR, "这个有可能异常~");
        // }

        // if (!"zhangsan".equals(article.getAuthor())) {
        //     throw new CustomException(CustomExceptionType.OTHER_ERROR);
        // }

        // 断言异常
        Assert.isTrue("zhangsan".equals(article.getAuthor()), "我是自己断言的异常，会抛出 IllegalArgumentException 这个异常的捕获在全局异常");

        return AjaxResponse.success();
    }


}
